/*
 * @Descripttion : radio
 * @Versions     : 0.1
 * @Author       : foxui team
 * @Date         : 2022-02-24 20:12:41
 * @LastEditors  : foxui team
 * @LastEditTime : 2022-02-24 21:10:28
 */

function Radio() {
    this.HANDLE = '.foxui-radio:not(.is-disabled)';
    this.CONTAINER = '.foxui-radio-group';
    this.RADIO = '.foxui-radio';
}

Radio.prototype._init = function () {
    this._bindToggle();
};

Radio.prototype._bindToggle = function () {
    let that = this;
    $(document).on('click', this.HANDLE, function () {
        let $this = $(this);
        let isChecked = $this.is('.is-checked');
        if (!isChecked) {
            let $radios = $this.closest(that.CONTAINER).find(that.RADIO);
            // 遍历父组下 radio 并作相应操作
            $radios.removeClass('is-checked');
            $radios.find('input').prop('checked', false);
            // 操作当前 radio
            $this.addClass('is-checked');
            $this.find('input').prop('checked', true);
        }
    });
};

let radio = new Radio();

export { radio };
